/*
# Copyright (c) 2020 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
*/

#ifndef MAX30102_H
#define MAX30102_H
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_errno.h"

#include "hi_io.h"
#include "hi_gpio.h"
#include "hi_time.h"
#include "hi_i2c.h"

#define MAX_SDA_IO0 0
#define MAX_SCL_IO1 1
#define MAX_I2C_IDX 1
#define MAX_I2C_BAUDRATE ((400)*(1000))

#define MAX30102_WR_address 0xAE
#define I2C_WRITE_ADDR 0xAE
#define I2C_READ_ADDR 0xAF

#define INTR_STATUS_1 0x00
#define INTR_STATUS_2 0x01
#define INTR_ENABLE_1 0x02
#define INTR_ENABLE_2 0x03
#define FIFO_WR_PTR 0x04
#define OVF_COUNTER 0x05
#define FIFO_RD_PTR 0x06
#define FIFO_DATA 0x07
#define FIFO_CONFIG 0x08
#define MODE_CONFIG 0x09
#define SPO2_CONFIG 0x0A
#define LED1_PA 0x0C
#define LED2_PA 0x0D
#define PILOT_PA 0x10
#define MULTI_LED_CTRL1 0x11
#define MULTI_LED_CTRL2 0x12
#define TEMP_INTR 0x1F
#define TEMP_FRAC 0x20
#define TEMP_CONFIG 0x21
#define PROX_INT_THRESH 0x30
#define REV_ID 0xFE
#define PART_ID 0xFF

/**
 * @brief Send Write command to MAX30102 device before Read a register.
 * @param regAddr the register address to Read or Writen.
 * @return Returns{@link IOT_SUCCESS} if the operation is successful;
 * returns an error code defined in {@link iot_errno.h} otherwise.
 */
uint8_t MAX_Cmd(uint8_t regAddr);

/**
 * @brief Read a data byte from  MAX30102 device.
 * @param  regAddr the register address.  8bit data
 * @return *data
 */
uint32_t MAX_Read_Data(uint8_t regAddr, uint8_t *data, unsigned int dataLen);

/**
 * @brief Write a  byte data to MAX30102 device.
 * @param regAddr the register address.
 * data the data to writen
 * @return Returns{@link IOT_SUCCESS} if the operation is successful;
 * returns an error code defined in {@link iot_errno.h} otherwise.
 */
uint32_t MAX_Write_Data(uint8_t regAddr, uint8_t *data, unsigned int dataLen);

void max_init(void);
void max_reset(void);
/**
 * @brief read FIFO data in max FIFO register 0x07
 * @param RED_channel_data
 * @param IR_channel_data
 */
void max_Data(uint8_t *RED_channel_data, uint8_t *IR_channel_data);
void max_fifo(uint32_t *pun_red_led, uint32_t *pun_ir_led);
#endif
